package spring_annotation;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import spring_annotation.config.ComponentConfig;
import spring_annotation.config.MainLifeCycleConfig;
import spring_annotation.config.PersonConfig;
import spring_annotation.pojo.Car;
import spring_annotation.pojo.Cat;
import spring_annotation.pojo.Person;

import java.util.Map;


public class MySpringTest {

    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);

    @Test
    public void testImport(){
        printBeans(applicationContext);
    }

    private void printBeans(ApplicationContext applicationContext){
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
    }

    @Test
    public void test1() {
//        Person person = (Person) applicationContext.getBean("person");
        Person person = (Person) applicationContext.getBean("person02");
        System.out.println(person);
        String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String s : namesForType) {
            System.out.println(s);
        }
        Object obj1 = applicationContext.getBean("person02");
        Object obj2 = applicationContext.getBean("person02");
        System.out.println(obj1 == obj2);
    }
    @Test
    public void test2() {
        String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
        Environment environment = applicationContext.getEnvironment();
        String property = environment.getProperty("os.name");
        System.out.println(property);
        for (String s : namesForType) {
            System.out.println(s);
        }
        Map<String, Person> beansOfType = applicationContext.getBeansOfType(Person.class);
        System.out.println(beansOfType);
    }

    @Test
    public void test3(){
        Object colorFactoryBean = applicationContext.getBean("colorFactoryBean");
        Object colorFactoryBean1 = applicationContext.getBean("colorFactoryBean");
        Object colorFactoryBean2 = applicationContext.getBean("&colorFactoryBean");
        System.out.println(colorFactoryBean.getClass());
        System.out.println(colorFactoryBean2.getClass());
        System.out.println(colorFactoryBean == colorFactoryBean1);
    }

    @Test
    public void test4(){
        AnnotationConfigApplicationContext mlccApplicationContext =
                new AnnotationConfigApplicationContext(MainLifeCycleConfig.class);
        System.out.println("容器创建完成。。。");
        Car carBean = mlccApplicationContext.getBean(Car.class);
        Cat catBean = mlccApplicationContext.getBean(Cat.class);
        mlccApplicationContext.close();

    }

    @Test
    public void test5(){

    }

}
